<!-- movies/templates/movies/index.html -->
{% extends "./base.html" %}

{% block title %}电影列表{% endblock %}

{% block content %}
<!-- 在适当的位置添加搜索表单 -->
<form method="get" action="." class="search-form">
    <input type="text" name="q" value="{{ search_query }}" placeholder="搜索电影、导演、演员..." class="search-input">
    <button type="submit" class="search-button">搜索</button>
</form>

<!-- 显示搜索结果或所有电影 -->
<div class="container mt-4">
    <h1 class="mb-4">电影列表</h1>
    
    <div class="row">
        {% for movie in page_obj %}
        <div class="col-md-4 mb-4">
            <div class="card h-100">
                <img src="{{ movie.poster }}" class="card-img-top" alt="{{ movie.name }}" style="height: 400px; object-fit: cover;">
                <div class="card-body">
                    <h5 class="card-title">{{ movie.name }}</h5>
                    <p class="text-warning">
                        {% with ''|center:movie.rating as range %}
                        {% for _ in range %}
                            ★
                        {% endfor %}
                        {% endwith %}
                        <span class="text-muted">({{ movie.rating }})</span>
                    </p>
                    <p class="card-text">
                        <strong>导演:</strong> {{ movie.director }}<br>
                        <strong>主演:</strong> {{ movie.actors|truncatechars:30 }}<br>
                        <strong>年份:</strong> {{ movie.year }}<br>
                        <strong>地区:</strong> {{ movie.region }}<br>
                        <strong>类型:</strong> {{ movie.genre }}
                    </p>
                    <p class="card-text"><em>"{{ movie.quote }}"</em></p>
                </div>
            </div>
        </div>
        {% empty %}
            <p>没有找到匹配的电影。</p>
        {% endfor %}
    </div>

    <nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page=1" aria-label="First">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
                    <span aria-hidden="true">&lsaquo;</span>
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
            <li class="page-item {% if num == page_obj.number %}active{% endif %}">
                <a class="page-link" href="?page={{ num }}">{{ num }}</a>
            </li>
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
                    <span aria-hidden="true">&rsaquo;</span>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}" aria-label="Last">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>

</div>

{% endblock %}
